<?php
include_once("../Dao/db.php");
Class Excluded
{
	var $ins_text;
	var $search_text;	
	
	var $keyId;
	
	var $keyName="";
	
	var $groupId="";
	
	var $insRslt=0;
	
	var $show_deleted=0;
    
    function MyTask()
	{
		session_start();
		$this->iniForm();

	}
	
	function iniForm()
	{
		if(isset($_GET["groupId"])) 
		{
			$this->groupId = $_GET["groupId"];
		}
		else
		{
			header("Location:../Login.php");
			return;
		}
		if(isset($_POST["ins_text"])) 
		{
			$this->ins_text = $_POST["ins_text"];
			$this->insertKeyword();
		}
		if(isset($_POST["search_text"])) $this->search_text = $_POST["search_text"];
		if(isset($_POST["show_deleted"])) $this->show_deleted = $_POST["show_deleted"];
		if(isset($_POST["action"]) && $_POST["action"]=='DEL'){
			$this->deleteKeyword();
		}
		
	}
	function deleteKeyword(){
		$this->insRslt = 2;	
		$sql = " update keyword set status=9 ,sync=1 where key_id in ";
		$sql .= "(".$_POST["del_ids"].") and "." memberId = ".$_SESSION["memberId"]." ";
		echo '<!--'.$sql.'-->';
		$result = mysql_query($sql);
		if (mysql_affected_rows() < 1){
			$this->insRslt = -2;
		}
		
	}
	function insertKeyword(){
		$sql = " select camp_Id from adgroup where ";
		$sql .= " memberId = ".$_SESSION["memberId"]." ";
		$sql .=" and status<>9 and adGr_Id =".$this->groupId ;
		$result = mysql_query($sql);
		$campId = -1;
		while ($row = mysql_fetch_array($result)) {
			$campId = $row[0];
		}
		if($campId == -1){
			$this->insRslt = -1;
		}else{
			$this->insRslt = 1;	
			$sql = " INSERT INTO keyword (memberId,listingType,camp_Id,adGr_Id,keywordId,";
			$sql .= "destinationUrl,redirectUrl,exemptionRequest,maxCpc,negative,paused,";
			$sql .= "status,text,replaceText,type,sync,registDate,lastUpDate) ";
			$sql .= " VALUES (".$_SESSION["memberId"].", '1', ".$campId.",".$this->groupId.", null,";
			$sql .= "'','','',0,1,0,";
			$sql .= " 1,'".$this->ins_text."', NULL,1,2, NOW(),NOW())";
		}
		echo '<!--'.$sql.'-->';
		$result = mysql_query($sql);
		if (mysql_affected_rows() < 1){
			$this->insRslt = -1;
		}	
	}
	
	function getSelectSqlForExclude()
	{
		$html = "";
		
		$sql = " select campaign.Name, adgroup.Name, keyword.text,keyword.sync,keyword.key_id,keyword.status ";
		$sql .= " FROM campaign, adgroup, keyword ";
		$sql .= " where campaign.memberId = ".$_SESSION["memberId"]." ";
		$sql .= " AND campaign.camp_Id = adgroup.camp_Id ";
		$sql .= " AND keyword.adGr_Id = adgroup.adGr_Id ";
		$sql .= " AND keyword.negative = 1";
		$sql.= " and adgroup.adGr_Id =".$this->groupId;
		if($this->show_deleted == 0) $sql.= " and keyword.status <> 9 ";
		if($this->search_text != "") $sql.= " and keyword.text like '%".$this->search_text."%'";
		print '<!--'.$sql.'-->';
		$result = mysql_query($sql);
		$rows = mysql_num_rows($result);
		
		if ($rows > 0){
			$html.=	'<table class="tbl" width="100%" border="0" cellpadding="4" cellspacing="0">';
			$html.=	'<tbody><tr class="tr2" align="center"><td width="40" align="center">No</td><td width="12"></td>';
			$html.=	'<td align="left">キャンペーン</td><td align="left">広告グループ</td>';
			$html.=	'<td align="left">除外ワード</td>';
			$html.=	'<td align="center"><input type="checkbox" onclick="check_all(this.checked, \'del\')" id="del_all"></td></tr>';
			
			for ($i=0;$i<$rows;$i++)
			{
				$data = mysql_fetch_row($result);
				if($i%2 == "0"){
						$html .= ' <tr class="tr4">';
				}else{
					$html .= ' <tr class="tr3">';
				}
				$html.=	'<td align="center">'.($i+1).'</td>';
				if($data[5] == 9){
					$html.=	'<td align="center"><img src="../images/sync_deleted.gif" title="削除" border="0" /></td>';
				}else if($data[3] == 1){
					$html.=	'<td align="center"><img src="../images/sync_on.gif" title="同期済" border="0" /></td>';
				}else if($data[3] == 2){
					$html.=	'<td align="center"><img src="../images/sync_updating.gif" title="同期待ち" border="0" /></td>';
				}else if($data[3] == 3){
					$html.=	'<td align="center"><img src="../images/sync_error.gif" title="同期エラー" border="0" /></td>';
				}else {
					$html.=	'<td align="center"><img src="../images/sync_updating.gif" title="同期待ち" border="0" /></td>';
				}
				$html.=	'<td align="left">'.$data[0].'</td>';
	    		$html.=	'<td align="left">'.$data[1].'</td>';
	    		$html.=	'<td align="left">'.$data[2].'</td>';
				$html.=	'<td align="center"><input type="checkbox" name="del" value="'.$data[4].'" onclick="change_check(\'del\', \'del_all\');"></td></tr>';
			}
			$html.=	'</tbody></table>';
			$html.=	'<table width="100%" border="0" cellpadding="2" cellspacing="0">';
			$html .= '<tr><td align="left"></td><td align="right"> 1件-'.$rows.'件/ '.$rows.'件&nbsp;&nbsp; </td></tr></table>';
		} else {
				$html .= "データはありません。";
		}
		echo $html;
		
	}	
 
}
?>